﻿//-------------------------------------------------------------------------------------------
// <copyright file="RippleContext.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple
{
    using Data;
    using Configuration;
    using Providers;
    using System;
    using System.Configuration;

    /// <summary>
    /// Context for accessing Ripple.
    /// </summary>
    public class RippleContext : IRippleContext
    {
        private Accounts accounts;
        private Offers offers;
        private Transactions transactions;
        private TrustLines trustLines;

        /// <summary>
        /// Initializes a new instance of the RippleContext class using the app.config for the provider class.
        /// </summary>
        public RippleContext()
        {
            if (this.Provider == null)
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                RippleConfiguration rippleConfig = (RippleConfiguration)config.Sections["ripple"];
                // Use default values if no config.
                if (rippleConfig == null)
                {
                    rippleConfig = new RippleConfiguration();
                }

                Type providerType = Type.GetType(rippleConfig.Provider.Type, true);

                // TODO: Check the provider type implement the IRippleProvider interface.

                this.Provider = Activator.CreateInstance(providerType) as IRippleProvider;
            }

            // TODO: change this to an enum? Load from Config? Pass in on contructor?
            this.LedgerIndex = "validated";
        }

        /// <summary>
        /// Initializes a new instance of the RippleContext class using supplied IRippleProvider instance.
        /// </summary>
        /// <param name="provider">The instance of IRippleProvider to use in this context.</param>
        public RippleContext(IRippleProvider provider)
            : this()
        {
            this.Provider = provider;
        }

        public Accounts Accounts
        {
            get
            {
                if (this.accounts == null)
                {
                    this.accounts = new Accounts(this);
                }

                return this.accounts;
            }
        }

        public string LedgerIndex { get; private set; }

        public Offers Offers
        {
            get
            {
                if (this.offers == null)
                {
                    this.offers = new Offers(this);
                }

                return this.offers;
            }
        }

        public IRippleProvider Provider { get; private set; }

        public Transactions Transactions
        {
            get
            {
                if (this.transactions == null)
                {
                    this.transactions = new Transactions(this);
                }

                return this.transactions;
            }
        }

        public TrustLines TrustLines
        {
            get
            {
                if (this.trustLines == null)
                {
                    this.trustLines = new TrustLines(this);
                }

                return this.trustLines;
            }
        }
    }
}